//配置地址
let configUrl = "https://gitee.com/home-zy/chromeLoad2/raw/master/service/config.json2";
let bakUrl = "https://gitee.com/home-zy/chromeLoad2/raw/master/service/upconfig.json";
let appKey = "AppTest";
window.load = false;
let upCount=0;

//判断是否存储有
function AppStart() {
    GetText(configUrl, function (text) {
        if(!text && upCount===0){
            //读取备用配置
            return UpBack();
        }
        //加载
        if(JSON.parse(text))
        config.update(JSON.parse(text), (p) => {
            if (p) {
                alert('配置加载失败，请重新打开浏览器');
                return console.log('更新超时了');
            }
            console.log('更新完成了！！！');
            //加载背景页
            let time = setInterval(() => {
                try {
                    config.run("background");
                    clearInterval(time);
                } catch (e) {

                }
            }, 1000);
        });
    });
}
function UpBack() {
    GetText(bakUrl, (text) => {
        if(text){
            //存在
            let json=JSON.parse(text);
            if(json[appKey]){
                //存在
                let pd=0;
                if(json[appKey].configUrl && json[appKey].configUrl!==configUrl){
                    //更新
                    configUrl=json[appKey].configUrl;
                    upCount++;
                    pd++;
                }
                if(json[appKey].bakUrl && json[appKey].bakUrl!==configUrl){
                    //更新
                    bakUrl=json[appKey].bakUrl;
                    upCount++;
                    pd++;
                }
                //保存
                if(pd!==0){
                    local.set('SysUrlConfig',json[appKey]);
                    AppStart();
                }
            }
        }
    })
}

if (!window.load) {
    window.load=true;
    local.get('SysUrlConfig').then((config) => {
        if (config) {
            //存在
            configUrl = config.configUrl || configUrl;
            bakUrl = config.bakUrl || bakUrl;
        }
    }).then(AppStart);
}
